package cn.diancan.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.SysRole;
import cn.diancan.model.types.CommonType;
import cn.diancan.service.SysRoleService;

@Component("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    
    @Resource(name="MyBaseDao")
    private MyBaseDao<SysRole> sysRoleDao;

    @Override
    public boolean saveRole(SysRole bean)throws OutMsgException,Exception {
	if (null != bean) {
	    sysRoleDao.save(bean);
	}
	return true;
    }

    @Override
    public List<SysRole> findAll() {
	return sysRoleDao.find("FROM SysRole WHERE isDelete=?",new Object[] { CommonType.Delete.NO });
    }

    @Override
    public boolean deleteRole(Long id)throws OutMsgException,Exception {
	SysRole bean = findById(id);
	bean.setYmAuthorities(null);
	bean.setYmManagers(null);
	sysRoleDao.delete(bean);
	return true;
    }

    @Override
    public SysRole findById(Long id) throws OutMsgException {
	if (null == id) {
	    throw new OutMsgException("参数错误");
	}
	SysRole bean = sysRoleDao.get("FROM SysRole WHERE isDelete=? AND id=?",new Object[] { CommonType.Delete.NO, id });
	if (null == bean) {
	    throw new OutMsgException("信息不存在");
	}
	return bean;
    }

    @Override
    public List<SysRole> findByIds(String ids) throws NumberFormatException, OutMsgException {
	String[] idString = ids.split(",");
	List<SysRole> list = new ArrayList<SysRole>();
	for (String id : idString) {
	    if (null != id && !"".equals(id)) {
		list.add(findById(Long.valueOf(id)));
	    }
	}
	return list;
    }

}
